package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.base.entity.RoastingDesulphurizing;
import com.hkts.naturalgas.base.entity.RoastingDust;
import com.hkts.naturalgas.base.entity.RoastingPurificationBleaching;
import com.hkts.naturalgas.base.mapper.RoastingDustMapper;
import com.hkts.naturalgas.base.service.IRoastingDustService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class RoastingDustServiceImpl extends ServiceImpl<RoastingDustMapper, RoastingDust> implements IRoastingDustService {


    @Autowired
    private RoastingDustMapper roastingDustMapper;

    @Override
    public IPage<RoastingDust> selectPage(Page<RoastingDust> page, RoastingDust roastingDust) {
        QueryWrapper<RoastingDust> query = Wrappers.query();
        QueryWrapper<RoastingDust> entityWrapper = getEntityWrapper(query, roastingDust);
        return roastingDustMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<RoastingDust> selectList(RoastingDust roastingDust) {
        QueryWrapper<RoastingDust> query = Wrappers.query();
        QueryWrapper<RoastingDust> entityWrapper = getEntityWrapper(query, roastingDust);
        return roastingDustMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingDust> getEntityWrapper(QueryWrapper<RoastingDust> entityWrapper, RoastingDust roastingDust) {
        //条件拼接
        return entityWrapper;
    }
    @Override
    public IPage<RoastingDust> selectPageByTime(Page<RoastingDust> page, String startDate, String endDate){
        QueryWrapper<RoastingDust> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingDust::getTime);

        IPage<RoastingDust> hktsTailGasPage = roastingDustMapper.selectPage(page, query);
        return hktsTailGasPage;
    }

    @Override
    public IPage<RoastingDust> getBrokenLineGraph(Page<RoastingDust> page, String startDate, String endDate){
        QueryWrapper<RoastingDust> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingDust::getTime);
        IPage calcinationDesulfurizationDenitrations = roastingDustMapper.selectPage(page,query);
        List<RoastingDust> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingDust vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("喷淋塔进口温度(℃)",vo.getBsjhAi1());
        data.put("喷淋塔出口温度(℃)",vo.getBsjhAi2());
        data.put("1电场一次电压(V)",vo.getBsjhHvc140067());
        data.put("1电场一次电流(A)",vo.getBsjhHvc140068());
        data.put("2电场一次电压(V)",vo.getBsjhHvc240067());
        data.put("2电场一次电流(A)",vo.getBsjhHvc240068());
        data.put("3电场一次电压(V)",vo.getBsjhHvc340067());
        data.put("3电场一次电流(A)",vo.getBsjhHvc340068());
        data.put("4电场一次电压(V)",vo.getBsjhHvc440067());
        data.put("4电场一次电流(A)",vo.getBsjhHvc440068());
        data.put("5电场一次电压(V)",vo.getBsjhHvc540067());
        data.put("5电场一次电流(A)",vo.getBsjhHvc540068());
        data.put("6电场一次电压(V)",vo.getBsjhHvc640067());
        data.put("6电场一次电流(A)",vo.getBsjhHvc640068());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void deleteAll(List<String> collect) {
        roastingDustMapper.deleteAllData();
    }

    @Override
    public void dataNeatHndler() {
        QueryWrapper<RoastingDust> query = Wrappers.query();
        query.le(RoastingDust.TIME,"2021-5-22 19:30:00");
        List<RoastingDust> roastingDusts = roastingDustMapper.selectList(query);
        System.out.println("size:"+roastingDusts.size());
        for(int i=0;i<roastingDusts.size();i++){
            roastingDusts.get(i).setBsjhHvc640068(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc640068()));
            roastingDusts.get(i).setBsjhHvc640067(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc640067()));
            roastingDusts.get(i).setBsjhHvc540067(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc540067()));
            roastingDusts.get(i).setBsjhHvc540068(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc540068()));
            roastingDusts.get(i).setBsjhHvc440068(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc440068()));

            roastingDusts.get(i).setBsjhHvc440067(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc440067()));
            roastingDusts.get(i).setBsjhHvc340068(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc340068()));
            roastingDusts.get(i).setBsjhHvc340067(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc340067()));
            roastingDusts.get(i).setBsjhHvc240068(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc240068()));
            roastingDusts.get(i).setBsjhHvc240067(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc240067()));
            roastingDusts.get(i).setBsjhHvc140068(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc140068()));
            roastingDusts.get(i).setBsjhHvc140067(DataHandlerUtils.getRundomInt(roastingDusts.get(i).getBsjhHvc140067()));
        }
        this.saveOrUpdateBatch(roastingDusts) ;

    }

    @Override
    public void dataBugHndler() {
        QueryWrapper<RoastingDust> query = Wrappers.query();
        query.le(RoastingDust.TIME,"2021-06-10 10:06:48");
        List<RoastingDust> roastingDusts = roastingDustMapper.selectList(query);
        float bsjhHvc640068=0f;
        float bsjhHvc640067=0f;
        float bsjhHvc540068=0f;
        float bsjhHvc540067=0f;
        float bsjhHvc440068=0f;
        float bsjhHvc440067=0f;
        float bsjhHvc340068=0f;
        float bsjhHvc340067=0f;
        float bsjhHvc240068=0f;
        float bsjhHvc240067=0f;
        for(int i=0;i<roastingDusts.size();i++){
            bsjhHvc640068=roastingDusts.get(i).getBsjhHvc640068();
            bsjhHvc640067=roastingDusts.get(i).getBsjhHvc640067();
            bsjhHvc540068=roastingDusts.get(i).getBsjhHvc540068();
            bsjhHvc540067=roastingDusts.get(i).getBsjhHvc540067();
            bsjhHvc440068=roastingDusts.get(i).getBsjhHvc440068();
            bsjhHvc440067=roastingDusts.get(i).getBsjhHvc440067();
            bsjhHvc340068=roastingDusts.get(i).getBsjhHvc340068();
            bsjhHvc340067=roastingDusts.get(i).getBsjhHvc340067();
            bsjhHvc240068=roastingDusts.get(i).getBsjhHvc240068();
            bsjhHvc240067=roastingDusts.get(i).getBsjhHvc240067();
            roastingDusts.get(i).setBsjhHvc640068(bsjhHvc640067);
            roastingDusts.get(i).setBsjhHvc640067(bsjhHvc640068);
            roastingDusts.get(i).setBsjhHvc540068(bsjhHvc540067);
            roastingDusts.get(i).setBsjhHvc540067(bsjhHvc540068);
            roastingDusts.get(i).setBsjhHvc440068(bsjhHvc440067);
            roastingDusts.get(i).setBsjhHvc440067(bsjhHvc440068);
            roastingDusts.get(i).setBsjhHvc340068(bsjhHvc340067);
            roastingDusts.get(i).setBsjhHvc340067(bsjhHvc340068);
            roastingDusts.get(i).setBsjhHvc240068(bsjhHvc240067);
            roastingDusts.get(i).setBsjhHvc240067(bsjhHvc240068);
        }
        this.saveOrUpdateBatch(roastingDusts);
    }
}
